package com.vgemv.jsutilitysdkdemo.module.Menu.SlowLive;


import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemView.JsMenuItemView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVm;


public class SlowLiveItemView extends JsMenuItemView {
    public ImageView miv;
    public TextView mtv_title;


    private LinearLayout mll_root;

    public SlowLiveItemView(Context context) {
        super(context);
    }


    @Override
    public void customSetVm(Context context, JsMenuItemVm vm) {
        if(mll_root == null) {
            mll_root = new LinearLayout(context);
            mll_root.setOrientation(LinearLayout.VERTICAL);
            addView(mll_root);
            mll_root.setGravity(Gravity.CENTER);
        }

        {
            //miv
            if(miv == null) {
                miv = new ImageView(getContext());
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, SlowLiveVm.imageHeight);
                miv.setLayoutParams(lp);
                mll_root.addView(miv);
            }
        }
        {
            if(mtv_title == null) {
                mtv_title = new TextView(getContext());
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, SlowLiveVm.labelHeight);
                mtv_title.setLayoutParams(lp);
                mll_root.addView(mtv_title);
                mtv_title.setTextSize(20);
                mtv_title.setGravity(Gravity.CENTER);
                mtv_title.setTextAlignment(TEXT_ALIGNMENT_CENTER);
            }
        }
        vm.getUnselectedStyle().mi_width = SlowLiveVm.itemViewWidth;
        vm.getUnselectedStyle().mi_height = SlowLiveVm.itemViewHeight;
        customUpdateUi(mvm);
    }



    public void customUpdateUi(JsMenuItemVm vm) {
        SlowLiveVm slowLiveVm = (SlowLiveVm)vm;
        //根据vm进行显示
        {
                Picasso.get().load(slowLiveVm.imageUrlString)
                        .centerCrop()//设置scaleType为centerCrop，充满ImageView 的边界，居中裁剪
                            .placeholder(com.vgemv.jsutilitysdk.R.mipmap.placeholder)//网络请求还没有完成时显示的本地图片
                        .error(com.vgemv.jsutilitysdk.R.mipmap.placeholder)
                        .fit()//fit 它会自动测量我们的View的大小，然后内部调用reszie方法把图片裁剪到View的大小(fit 还是会出现拉伸扭曲的情况，因此最好配合前面的centerCrop使用)
                        //设置内存缓存策略为不缓存,默认缓存,不然每次都需要重新加载
//                        .memoryPolicy(MemoryPolicy.NO_CACHE,MemoryPolicy.NO_STORE)
                        .into(miv);//显示到指定的控件上
        }
//            miv.setBackgroundColor(Color.parseColor("#ffff0000"));
        {
            mtv_title.setText(slowLiveVm.title);
            if(vm.mb_isSelected) {
                mtv_title.setTextColor(Color.argb(255, 0, 0, 0));
                mtv_title.setBackgroundColor(Color.argb(255,216,202,175));
                mtv_title.setTextColor(Color.argb(255,255,255,255));
                vm.getUnselectedStyle().mi_borderWidth = 4;
                vm.getUnselectedStyle().mI_borderColor = Color.argb(255,216,202,175);
            }
            else {
                mtv_title.setTextColor(Color.argb(255, 0, 0, 0));
                mtv_title.setBackgroundColor(Color.argb(255,255,255,255));
                mtv_title.setTextColor(Color.argb(255,0,0,0));
                vm.getUnselectedStyle().mi_borderWidth = 2;
                vm.getUnselectedStyle().mI_borderColor = Color.argb(255,200,200,200);
            }
        }
    }
}